<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="源单据" prop="origin">
        <el-input
          v-model="queryParams.origin"
          placeholder="请输入源单据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="daterangeDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司id" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置id" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入位置id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="productId" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入productId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包装id" prop="packageId">
        <el-input
          v-model="queryParams.packageId"
          placeholder="请输入包装id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作伙伴id" prop="partnerId">
        <el-input
          v-model="queryParams.partnerId"
          placeholder="请输入合作伙伴id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Inventoried Lot/Serial Number" prop="lotId">
        <el-input
          v-model="queryParams.lotId"
          placeholder="请输入Inventoried Lot/Serial Number"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Inventory of" prop="filter">
        <el-input
          v-model="queryParams.filter"
          placeholder="请输入Inventory of"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Inventoried Category" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入Inventoried Category"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Include Exhausted Products" prop="exhausted">
        <el-input
          v-model="queryParams.exhausted"
          placeholder="请输入Include Exhausted Products"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者id" prop="createUid">
        <el-input
          v-model="queryParams.createUid"
          placeholder="请输入创建者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新者id" prop="writeUid">
        <el-input
          v-model="queryParams.writeUid"
          placeholder="请输入更新者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期" prop="writeDate">
        <el-date-picker clearable
          v-model="queryParams.writeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="Force Accounting Date">
        <el-date-picker
          v-model="daterangeAccountingDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="源单据" prop="origin">
        <el-input
          v-model="queryParams.origin"
          placeholder="请输入Origin"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到期日期">
        <el-date-picker
          v-model="daterangeArrivalDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="Shipment Date" prop="shipmentDate">
        <el-date-picker clearable
          v-model="queryParams.shipmentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择Shipment Date">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="Package Num" prop="packageNum">
        <el-input
          v-model="queryParams.packageNum"
          placeholder="请输入Package Num"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单" prop="saleOrderId">
        <el-input
          v-model="queryParams.saleOrderId"
          placeholder="请输入销售订单"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:inventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:inventory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:inventory:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange" @row-click="handleDetail">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pick_state" :value="scope.row.state" />
        </template>
        </el-table-column>
      <!-- <el-table-column label="公司id" align="center" prop="companyId" /> -->
      <!-- <el-table-column label="位置id" align="center" prop="locationId" /> -->
      <!-- <el-table-column label="productId" align="center" prop="productId" /> -->
      <!-- <el-table-column label="包装id" align="center" prop="packageId" /> -->
      <!-- <el-table-column label="合作伙伴id" align="center" prop="partnerId" /> -->
      <!-- <el-table-column label="Inventoried Lot/Serial Number" align="center" prop="lotId" /> -->
      <!-- <el-table-column label="Inventory of" align="center" prop="filter" /> -->
      <!-- <el-table-column label="Inventoried Category" align="center" prop="categoryId" /> -->
      <!-- <el-table-column label="Include Exhausted Products" align="center" prop="exhausted" /> -->
      <!-- <el-table-column label="创建者id" align="center" prop="createUid" /> -->
      <!-- <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者id" align="center" prop="writeUid" /> -->
      <!-- <el-table-column label="更新日期" align="center" prop="writeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="Force Accounting Date" align="center" prop="accountingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accountingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="源单据" align="center" prop="origin" />
      <!-- <el-table-column label="到期日期" align="center" prop="arrivalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="Shipment Date" align="center" prop="shipmentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shipmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="Package Num" align="center" prop="packageNum" /> -->
      <!-- <el-table-column label="销售订单" align="center" prop="saleOrderId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-deocument"
            @click="handleDetail(scope.row)"
            
          >详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inventory:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inventory:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Inventory对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="公司id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司id" />
        </el-form-item>
        <el-form-item label="位置id" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入位置id" />
        </el-form-item>
        <el-form-item label="productId" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入productId" />
        </el-form-item>
        <el-form-item label="包装id" prop="packageId">
          <el-input v-model="form.packageId" placeholder="请输入包装id" />
        </el-form-item>
        <el-form-item label="合作伙伴id" prop="partnerId">
          <el-input v-model="form.partnerId" placeholder="请输入合作伙伴id" />
        </el-form-item>
        <el-form-item label="Inventoried Lot/Serial Number" prop="lotId">
          <el-input v-model="form.lotId" placeholder="请输入Inventoried Lot/Serial Number" />
        </el-form-item>
        <el-form-item label="Inventory of" prop="filter">
          <el-input v-model="form.filter" placeholder="请输入Inventory of" />
        </el-form-item>
        <el-form-item label="Inventoried Category" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入Inventoried Category" />
        </el-form-item>
        <el-form-item label="Include Exhausted Products" prop="exhausted">
          <el-input v-model="form.exhausted" placeholder="请输入Include Exhausted Products" />
        </el-form-item>
        <el-form-item label="创建者id" prop="createUid">
          <el-input v-model="form.createUid" placeholder="请输入创建者id" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者id" prop="writeUid">
          <el-input v-model="form.writeUid" placeholder="请输入更新者id" />
        </el-form-item>
        <el-form-item label="更新日期" prop="writeDate">
          <el-date-picker clearable
            v-model="form.writeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Force Accounting Date" prop="accountingDate">
          <el-date-picker clearable
            v-model="form.accountingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Force Accounting Date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="源单据" prop="origin">
          <el-input v-model="form.origin" placeholder="请输入Origin" />
        </el-form-item>
        <el-form-item label="到期日期" prop="arrivalDate">
          <el-date-picker clearable
            v-model="form.arrivalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Shipment Date" prop="shipmentDate">
          <el-date-picker clearable
            v-model="form.shipmentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择Shipment Date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Package Num" prop="packageNum">
          <el-input v-model="form.packageNum" placeholder="请输入Package Num" />
        </el-form-item>
        <el-form-item label="销售订单" prop="saleOrderId">
          <el-input v-model="form.saleOrderId" placeholder="请输入销售订单" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory } from "@/api/system/inventory";
import inventorydetail from '@/views/system/inventorydetail/index.vue'
export default {
  name: "Inventory",
  dicts: ['pick_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Inventory表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 销售订单时间范围
      daterangeDate: [],
      // 销售订单时间范围
      daterangeCreateDate: [],
      // 销售订单时间范围
      daterangeAccountingDate: [],
      // 销售订单时间范围
      daterangeArrivalDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        date: null,
        state: null,
        companyId: null,
        locationId: null,
        productId: null,
        packageId: null,
        partnerId: null,
        lotId: 1,
        filter: null,
        categoryId: null,
        exhausted: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        accountingDate: null,
        origin: null,
        arrivalDate: null,
        shipmentDate: null,
        packageNum: null,
        saleOrderId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        companyId: [
          { required: true, message: "公司id不能为空", trigger: "blur" }
        ],
        locationId: [
          { required: true, message: "位置id不能为空", trigger: "blur" }
        ],
        filter: [
          { required: true, message: "Inventory of不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询Inventory列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDate && '' != this.daterangeDate) {
        this.queryParams.params["beginDate"] = this.daterangeDate[0];
        this.queryParams.params["endDate"] = this.daterangeDate[1];
      }
      if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      if (null != this.daterangeAccountingDate && '' != this.daterangeAccountingDate) {
        this.queryParams.params["beginAccountingDate"] = this.daterangeAccountingDate[0];
        this.queryParams.params["endAccountingDate"] = this.daterangeAccountingDate[1];
      }
      if (null != this.daterangeArrivalDate && '' != this.daterangeArrivalDate) {
        this.queryParams.params["beginArrivalDate"] = this.daterangeArrivalDate[0];
        this.queryParams.params["endArrivalDate"] = this.daterangeArrivalDate[1];
      }
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        date: null,
        state: null,
        companyId: null,
        locationId: null,
        productId: null,
        packageId: null,
        partnerId: null,
        lotId: 1,
        filter: null,
        categoryId: null,
        exhausted: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        accountingDate: null,
        origin: null,
        arrivalDate: null,
        shipmentDate: null,
        packageNum: null,
        saleOrderId: null
      };
      this.resetForm("form");
    },
    // 详情
    handleDetail(row) {
      const id = row.id
      this.$router.push({
        path: "/inventorydetail",
        component: inventorydetail,
        redirect: "noRedirect",
        hidden: false,
        alwaysShow: true,
        query: { listId: id },
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDate = [];
      this.daterangeCreateDate = [];
      this.daterangeAccountingDate = [];
      this.daterangeArrivalDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Inventory";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Inventory";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除Inventory编号为"' + ids + '"的数据项？').then(function() {
        return delInventory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
